def p(n):
    for i in range (2, n):
        if n % i == 0:
            return False
    return True

    
number = int(input())

o = [ i for i in range (1,10000) if p (i)]

m = 0
if 1000<=number<=9999:
             for i in o:
                 if str(i) in str(number):
                     m = max(i,m)
print(m)
